﻿using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
using System.Xml;
using Microsoft.Windows.Controls.Ribbon;
using WebsiteLoadTool.Configuration;

namespace WebsiteLoadTool
{
    public partial class MainView : RibbonWindow
    {
        private Version executingVersion;

        public MainView()
        {
            executingVersion = Assembly.GetExecutingAssembly().GetName().Version;

            InitializeComponent();

            Loaded += MainViewLoaded;
            Closing += MainViewClosing;
        }

        private void MainViewLoaded(object sender, RoutedEventArgs e)
        {
            if (DataContext is MainViewModel)
                ((MainViewModel)DataContext).Loaded();
        }

        private void MainViewClosing(object sender, CancelEventArgs e)
        {
            if (DataContext is MainViewModel)
                ((MainViewModel)DataContext).Unloaded();

            Loaded -= MainViewLoaded;
            Closing -= MainViewClosing;
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            if (!Properties.Settings.Default.FirstRun && string.Equals(executingVersion.ToString(), Properties.Settings.Default.Version.ToString()))
            {
                Width = Properties.Settings.Default.WindowSize.Width;
                Height = Properties.Settings.Default.WindowSize.Height;
                Left = Properties.Settings.Default.WindowLocation.X;
                Top = Properties.Settings.Default.WindowLocation.Y;
                WindowState = Properties.Settings.Default.WindowState;
                RibbonMenu.IsMinimized = Properties.Settings.Default.RibbonIsMinimized;
                RibbonMenu.ShowQuickAccessToolBarOnTop = Properties.Settings.Default.ShowQuickAccessToolbarOnTop;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Properties.Settings.Default.FirstRun = false;
            Properties.Settings.Default.Version = executingVersion.ToString();
            Properties.Settings.Default.WindowSize = new Size(Width, Height);
            Properties.Settings.Default.WindowLocation = new Point(Left, Top);
            Properties.Settings.Default.WindowState = WindowState;
            Properties.Settings.Default.RibbonIsMinimized = RibbonMenu.IsMinimized;
            Properties.Settings.Default.ShowQuickAccessToolbarOnTop = RibbonMenu.ShowQuickAccessToolBarOnTop;

            try
            {
                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                //TODO: Log error.
            }

            base.OnClosing(e);
        }

        private void LogTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            LogScrollViewer.ScrollToVerticalOffset(LogTextBlock.ActualHeight); 
        }
    }
}
